package com.fbinggun.dt.gui;

import com.fbinggun.dt.DailyTask;
import com.fbinggun.dt.player.PlayerData;
import com.fbinggun.dt.task.Task;
import com.fbinggun.fbinggunutil.ItemUtil;
import com.fbinggun.fbinggunutil.PApiUtil;
import com.fbinggun.fbinggunutil.gui.SlotItem;
import org.bukkit.Bukkit;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class ChoiceGui implements Listener {

    String title;
    int size;
    public  Map<String, ChoiceProject> choiceProjectHashMap = new HashMap<>();
    public Map<String, SlotItem> otherSlot= new HashMap<>();
    public Map<String,Map<Integer,String>> temp = new HashMap<>();
    public ChoiceGui(ConfigurationSection configurationSection) {
        this.title = configurationSection.getString("Title");
        size=configurationSection.getInt("Size");
        loadChoiceGui(configurationSection.getConfigurationSection("Gui"));
        DailyTask.plugin.getServer().getPluginManager().registerEvents(this,DailyTask.plugin);
    }


    private void loadChoiceGui(ConfigurationSection config) {
        choiceProjectHashMap = new HashMap<>();
        if (config != null) {
            for (String taskId : config.getKeys(false)) {
                ConfigurationSection configurationSection =config.getConfigurationSection(taskId);

                if(configurationSection.getBoolean("Other")){
                  otherSlot.put(taskId,new SlotItem(taskId,configurationSection));
                }else {
                    DailyTask.debug("加载选项"+taskId);
                choiceProjectHashMap.put(taskId, new ChoiceProject(taskId, configurationSection));}
            }
        }
    }
    public void openInventory(Player player) throws SQLException, ClassNotFoundException {
        PlayerData playerData = DailyTask.getPlayerData(player);
        if(playerData.getTask()!=null){
            new BukkitRunnable() {
                @Override
                public void run() {
                    player.closeInventory();
                    try {
                        openTaskGui(player);
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException(e);
                    }
                }
            }.runTask(DailyTask.plugin);

        }else {
            new BukkitRunnable() {
                @Override
                public void run() {
                    player.closeInventory();
                    try {
                        openChoiceGui(player);
                    } catch (SQLException e) {
                        throw new RuntimeException(e);
                    } catch (ClassNotFoundException e) {
                        throw new RuntimeException(e);
                    }
                }
            }.runTask(DailyTask.plugin);
        }
    }


    public void openChoiceGui(Player player) throws SQLException, ClassNotFoundException {
        Inventory inv = Bukkit.createInventory(null,size,title);
        PlayerData playerData = DailyTask.getPlayerData(player);
        Map<Integer,String> temp = new HashMap<>();
        for(ChoiceProject cp:choiceProjectHashMap.values()){
            if(player.hasPermission(cp.getPermission())) {
                Task task = playerData.getChoiceTask(cp);
                ItemStack taskItem =task.getItem(player).clone();
                if(cp.getLore()!=null) {
                    taskItem = ItemUtil.addLore(player,taskItem, PApiUtil.colorReplaceList(cp.getLore()));
                }
                temp.put(cp.getSlot(), cp.getId());
                inv.setItem(cp.getSlot(),taskItem);
            }
        }
        if(otherSlot!=null&&otherSlot.size()!=0){
            for(SlotItem item:otherSlot.values()){
                if(item.getPermission()!=null){
                if(!player.hasPermission(item.getPermission())){
                    continue;
                }}
                temp.put(item.getSlot(), item.getId());
                inv.setItem(item.getSlot(),item.getItem(player));
            }
        }
        this.temp.put(player.getName(),temp);
        player.openInventory(inv);

    }

    public void openTaskGui(Player player) throws SQLException, ClassNotFoundException {
         DailyTask.taskGui.openInventory(player);
    }
    @EventHandler
    public void click(InventoryClickEvent event) throws SQLException, ClassNotFoundException {
        Player player = (Player) event.getWhoClicked();
        Map<Integer,String> slot = temp.get(player.getName());
        if(slot==null){
            return;
        }
        event.setCancelled(true);
        String slotID= slot.get(event.getSlot());
        if(slotID==null){
            return;
        }
        ChoiceProject cp = choiceProjectHashMap.get(slotID);
        PlayerData playerData = DailyTask.getPlayerData(player);
        if(cp!=null){
            if(playerData.getTaskCount()>=DailyTask.plugin.getCount(player)){
                player.sendMessage(DailyTask.plugin.getLang("Task_FailGet_1"));
                return;
            }
            if(cp.getiPrice()!=null){
                if(cp.getiPrice().has(player)){
                    cp.getiPrice().take(player);
                    playerData.setTask(playerData.getChoiceTask(cp));
                    player.closeInventory();
                    openInventory(player);
                    player.sendMessage(DailyTask.plugin.getLang("TaskGet"));
                }else {
                player.sendMessage(DailyTask.plugin.getLang("Task_FailGet_Price"));
                return;
                }
            }else {
                playerData.setTask(playerData.getChoiceTask(cp));
                player.closeInventory();
                openInventory(player);
            }
        }
        SlotItem slotItem = otherSlot.get(slotID);
        if(slotItem!=null){
            slotItem.run(player,event,DailyTask.plugin);
        }

    }

    @EventHandler
    public void click(InventoryCloseEvent event) throws SQLException, ClassNotFoundException {
        Player player = (Player) event.getPlayer();
        Map<Integer,String> slot = temp.get(player.getName());
        if(slot==null){
            return;
        }
        temp.remove(player.getName());
    }
}
